package com.zhangtao.demo.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.imageclassify.AipImageClassify;

public class BaiduAipImageClassifyClient {
	private AipImageClassify client = null;

	public BaiduAipImageClassifyClient(String appId, String apiKey, String secretKey) {
		// 初始化一个AipImageClassifyClient
		client = new AipImageClassify(appId, apiKey, secretKey);
		// 可选：设置网络连接参数
		client.setConnectionTimeoutInMillis(2000);
		client.setSocketTimeoutInMillis(60000);

		// 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//		        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//		        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
	}

	public JSONObject advancedGeneralFilePath(String path) {
		JSONObject res = client.advancedGeneral(path, new HashMap<String, String>());
		System.out.println(res.toString(2));
		return res;
	}
	public JSONObject advancedGeneralByte(byte[] data) {
		JSONObject res = client.advancedGeneral(data, new HashMap<String, String>());
		System.out.println(res.toString(2));
		return res;
	}
	public static void base64ToFile(String destPath,String base64, String fileName) {
        File file = null;
        //创建文件目录
        String filePath=destPath;
        File  dir=new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            file=new File(filePath+"/"+fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
}
